<?php  
class ControllerSiteList extends Controller {
    public function index() {
		
        $this->document->setTitle($this->config->get('config_title').'站点列表');
		$this->document->setDescription($this->config->get('config_meta_description'));
		$this->data['heading_title'] = '站点列表';
        $this->data['opt_category'] = $this->outputCategoryList();
        $this->load->model('site');
        
        $allow_sort = array('pagerank,site_id');
        
        if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
        
        if (isset($this->request->get['cat_id'])) {
			$cat_id = $this->request->get['cat_id'];
		} else {
			$cat_id = 0;
		}        
        
        if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = '';
		}        
        
        if (isset($this->request->get['order'])) {
			if($this->request->get['order'] == 'DESC'){
                $order = 'DESC';
            } else {
                $order = 'ASC';
            }
		}
        $limit = '15';
        $data = array(
            'start' => ($page - 1) * $limit,
            'limit' => $limit,
            'sort'  => $sort,
            'order'  => $order,
            );
        if($cat_id)
        {
            $data['category'] = $cat_id ;
        }
        
        $this->data['sites'] = array();
        $site_total = $this->model_site->getTotalSites($data);
        $results = $this->model_site->getSites($data );
        if(!$results)
        {
            $this->data['text_empty'] = '没有记录';
        }

   		foreach ($results as $result) 
        {
			if ($result['status']) 
            {
				$this->data['sites'][] = array(
					'id'    => $result['site_id'],
					'url'   => $result['site_url'],
					'account'   => $result['account'],
					'other'   => $result['other'],
					'name'  => $result['site_name'],
                    'pagerank'   => $result['pagerank'],
					'cat_name' => $this->model_site->getSiteCatName($result['site_id']),
                    'total'    => $site_total
				);	
			}
		}
        
        $this->data['sorts'][] = array(
                'text'  => 'RP值',
				'value' => 'pagerank-ASC',
				'href'  => $this->url->link('site/list', '&sort=pagerank&order=ASC' . $url)
        );
        
        $this->data['sorts'][] = array(
                'text'  => '编号',
                'value' => 'site_id-ASC',
                'href'  => $this->url->link('site/list', '&sort=site_id&order=ASC' . $url)
        );
        
        $url = '';
        if (isset($this->request->get['cat_id'])) {
            $url .= '&cat_id=' . $this->request->get['cat_id'];
        }
        
        if (isset($this->request->get['sort'])) 
        {
            $url .= '&sort=' . $this->request->get['sort'];
        }
        
        $pagination = new Pagination();
		$pagination->total = $site_total;
		$pagination->page = $page;
		$pagination->limit = $limit;
		$pagination->text = '当前第 {start} 到 {end} 条 共 {total} 条 (共 {pages} 页)';
		$pagination->url = $this->url->link('site/list', $url. '&page={page}', 'NONSSL');
        $this->data['pagination'] = $pagination->render();
        
        $this->data['sort'] = $sort;
		$this->data['order'] = $order;
		$this->data['limit'] = $limit;
        
        
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . 'site/list.tpl')) {
			$this->template = $this->config->get('config_template') . 'site/list.tpl';
		} else {
			$this->template = 'site/list.tpl';
		}
		
		$this->children = array(
			'common/footer',
			'common/header'
		);
		$this->response->setOutput($this->render());
	}
    
    private function outputCategoryList()
    {
        $output = '';
        
        $this->load->model('category');
        $categories = $this->model_category->getCategories();

        foreach($categories as $category)
        {
            
            $output .= '<option value="' . $category['cat_id'] . '"';
           
            if (isset($this->request->get['cat_id']) && ($this->request->get['cat_id'] == $category['cat_id'])) {
	      		$output .= ' selected="selected"';
	    	}
	
	    	$output .= '>' . $category['cat_name'] . '</option>';
        }
		
		if (!$categories) {
		  	$output .= '<option value="0">没有选择项</option>';
		}
        
        return $output;
    }
}
?>